﻿namespace MuzoBlog.Core.Helpers
{
    using System;
    using System.Text;
    using System.Globalization;

    public class RandomHelper
    {
        const string alphabets = "abcdefghijklmnoprstuvwxyz";
        
        private static Random random = null;

        static RandomHelper()
        {
            random = new Random();
        }

        public static int GenerateInt32()
        {
            return GenerateInt32(int.MinValue, int.MaxValue);
        }

        public static int GenerateInt32(int minValue, int MaxValue)
        {
            return random.Next(minValue, MaxValue);
        }

        public static bool GenerateBool()
        {
            return random.NextDouble() > 0.5;
        }

        public static char GenerateChar(bool lowerCase)
        {
            string s = alphabets.Substring(GenerateInt32(0, alphabets.Length - 1), 1);
            if (!lowerCase) s = s.ToUpper(CultureInfo.InvariantCulture);

            return Convert.ToChar(s);
        }

        public static string GenerateString(int length)
        {
            StringBuilder stringBuilder = new StringBuilder(length);
            for (int i = 0; i < length; i++)
                stringBuilder.Append(GenerateChar(GenerateBool()));

            return stringBuilder.ToString();
        }

        public static string GeneratePassword(int length)
        {
            StringBuilder stringBuilder = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                if (GenerateBool())
                    stringBuilder.Append(GenerateChar(GenerateBool()));
                else
                    stringBuilder.Append(GenerateInt32(0,10));
            }

            return stringBuilder.ToString();
        }
    }
}
